import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { Utility, PeopleTaskList, Refresh } from 'components';
import * as CommonActions from 'redux/modules/reduxCommon';
import * as TaskActions from 'redux/modules/reduxTask';

@connect(
  state => ({
    Title: state.Common.Title,                                          // 标题
    UrlParams: state.Common.UrlParams,                                 // URL参数
    peopleList: state.TaskManager.peopleList,
  }),
  { ...CommonActions, ...TaskActions })

export default class ExecutivePeople extends Component {
  static propTypes = {
    UrlParams: PropTypes.object,
    onGetPeopleList: PropTypes.func,
    peopleList: PropTypes.object,
  };

  constructor(props) {
    super(props);
    this.state = {
      pgIndex: 0,
      detail: { content: '' },
      NextDataComplete: true,
      RefreshComplete: true,
      IsExistsNextData: true,
      peoplelist: {},
    };
  }

  componentDidMount() {
    Utility.$emit(Utility.constItem.Events.OnEditLeftAction, false);
    this.__firstGetList();
  }

  __onRefresh() {
  }

  __firstGetList() {
    const { UrlParams } = this.props;
    const taskId = UrlParams.taskId;
    const { onGetPeopleList } = this.props;
    const Condition = { IsExistsNextData: true };
    onGetPeopleList(taskId, Condition).then((result) => {
      if (result) {
        const {
         peopleList
       } = this.props;
        this.setState({ peopleList: peopleList });
      }
    }, () => {
      // this.setState({NextDataComplete: true});
    });
  }

  __onNextData() {
    const { onGetPeopleList } = this.props;
    const { peopleList } = this.state;
    const { Condition } = peopleList || { IsExistsNextData: true };
    if (!Condition.IsExistsNextData) {
      return;
    }
    this.setState({ NextDataComplete: false });
    const { UrlParams } = this.props;
    const taskId = UrlParams.taskId;
    onGetPeopleList(taskId, Condition).then((result) => {
      if (result) {
        this.setState({ peopleList: this.props.peopleList, NextDataComplete: true });
      } else {
        this.setState({ NextDataComplete: true });
      }
    }, () => {
      this.setState({ NextDataComplete: true });
    });
  }

  render() {
    const styles = require('./scss/ExecutivePeople.scss');
    const { RefreshComplete, NextDataComplete, peopleList } = this.state;
    const { List, Condition } = peopleList || {};
    const { IsExistsNextData } = Condition || {};
    return (
      <div className={styles.content}>
        <Refresh RefreshComplete={RefreshComplete} NextDataComplete={NextDataComplete} onRefresh={this.__onRefresh.bind(this)} onNextData={this.__onNextData.bind(this)}>
          <PeopleTaskList IsShowLoadingComplete={!IsExistsNextData} peopleTaskList={List} />
        </Refresh>
      </div >
    );
  }
}
